package com.lib.model.answer;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.lib.model.dto.AnswerDto;
import com.lib.model.dto.InformationDto;

public class AnswerBoardDao extends SqlMapClientDaoSupport implements
		IAnswerBoardDao {

	@Override
	public boolean addReply(AnswerDto dto) {
		int count = 0;
		try {
			getSqlMapClientTemplate().update("updateBoardStep",
					dto.getQuestionboard_num());
			getSqlMapClientTemplate().insert("addReply", dto);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public boolean deleteBoard(int questionboard_num) {
		int count = 0;

		try {
			getSqlMapClientTemplate().delete("deleteBoard", questionboard_num);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public AnswerDto getBoardDetail(int questionboard_num) {
		AnswerDto bdto = new AnswerDto();

		bdto = (AnswerDto) getSqlMapClientTemplate().queryForObject(
				"getBoardDetail", questionboard_num);
		getSqlMapClientTemplate().update("updateCount", questionboard_num);
		return bdto;
	}

	@Override
	public ArrayList<AnswerDto> getBoardList(int startPage, int startBlock,
			String searchType, String searchValue) {
		ArrayList<AnswerDto> list = new ArrayList<AnswerDto>();
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("startpage", startPage);
		map.put("endpage", startBlock);
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);

			list = (ArrayList<AnswerDto>) getSqlMapClientTemplate()
					.queryForList("getBoardList", map);

		return list;
	}

	@Override
	public boolean updateBoardDetail(AnswerDto udto) {
		int count = 0;

		try {
			getSqlMapClientTemplate().update("updateBoardDetail", udto);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public boolean writeBoard(AnswerDto dto) {
		int count = 0;
		try {
			getSqlMapClientTemplate().insert("writeBoard", dto);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public int SearchCount(String searchType, String searchValue) {

		int count = 0;

		Map<String, Object> map = new HashMap<String, Object>();

			count = (Integer) getSqlMapClientTemplate().queryForObject(
					"SearchCount", map);

		return count;
	}

	@Override
	public ArrayList<AnswerDto> homeQna() {
		// TODO Auto-generated method stub
		return (ArrayList<AnswerDto>)getSqlMapClientTemplate().queryForList("homeQna");
	}
}
